package com.cs.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cs.anno.Log;
import com.cs.domain.SysUser;
import com.cs.service.SysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

@RestController
@Api(tags = "后台用户的接口")
@RequestMapping("/sys/user")
public class SysUserController {
    @Autowired
    private SysUserService sysUserService;

    /**
     * 获得登录用户的信息
     * @return
     */
    @ApiOperation(value = "获得当前登录的用户信息")
    @GetMapping("/info")
    @Log(value = "获得登录用户的信息")
    public ResponseEntity<SysUser> getUserInfo(){
        //用户的id
        String userId = (String) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        SysUser sysUser = sysUserService.getById(userId);
        return ResponseEntity.ok(sysUser);

    }

    /**
     * 分页查询后台管理员
     * @return
     */
    @ApiOperation(value = "分页查询后台管理员的接口")
    @PreAuthorize("hasAuthority('sys:user:page')")
    @GetMapping("/page")
    @Log(value = "分页查询后台管理员")
    public ResponseEntity<Page<SysUser>> findSysUserPage(Page<SysUser> page,SysUser sysUser){
        Page<SysUser> sysUserPage=sysUserService.findSysUserPage(page,sysUser);
        return ResponseEntity.ok(sysUserPage);
    }

    /**
     * 添加管理员的接口
     * @param sysUser
     * @return
     */
    @PostMapping
    @PreAuthorize("hasAuthority('sys:user:save')")
    @ApiOperation(value = "添加管理员的接口")
    @Log(value = "添加管理员")
    public ResponseEntity<Void> addSysUser(@RequestBody SysUser sysUser){
        //设置操作人
        String userId = (String) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        sysUser.setCreateUserId(Long.valueOf(userId));
        sysUserService.addSysUser(sysUser);
        return ResponseEntity.ok().build();
    }
}
